import 'dart:typed_data';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../bean/GemBean.dart';
import '../generated/l10n.dart';
import '../main.dart';
import '../utils/CommUtils.dart';

/**
 * 游戏失败
 */

class TravelGameFile extends Dialog {
  int currentScore = 0; //当前分数
  CallBack? callback; //点击新游戏回调
  List<GemBean>? gemList;
  bool isgem = false; //1经典，2每日
  TravelGameFile(
      {required this.currentScore,
      required this.gemList,
      required this.callback,
      required this.isgem});
  @override
  Widget build(BuildContext context) {
    double size = CommUtils.getWidth(context);
    double hight = CommUtils.getHeight(context);
    return Material(
      type: MaterialType.transparency,
      child: Container(
        width: CommUtils.getISWidth(size, hight) ? hight * 0.7 : size * 0.85,
        margin: EdgeInsets.only(top: hight * 0.15),
        child: Column(
          children: [
            CommUtils.getTextGame(
              context,
              30,
              S.of(context).almost_,
              Color(0xfff6e8f9),
              Color(0Xffb68bcb),
            ),
            getBg(context, size, hight),
          ],
        ),
      ),
    );
  }

  Widget getBg(BuildContext context, double size, double hight) {
    return Container(
        margin: EdgeInsets.only(
            left: ScreenUtil().setWidth(15), right: ScreenUtil().setWidth(15)),
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage('assets/images/bg_seting.png'),
              fit: BoxFit.fill),
        ),
        child: Padding(
            padding: EdgeInsets.only(
                left: hight * 0.04,
                right: hight * 0.03,
                top: hight * 0.09,
                bottom: hight * 0.08),
            child: Column(children: [
              // SizedBox(
              //   height: ScreenUtil().setHeight(hight * 0.05),
              // ),
              Container(
                width: double.infinity,
                height: hight * 0.4,
                decoration: BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage('assets/images/middle.png'),
                      fit: BoxFit.fill),
                ),
                child: Padding(
                    padding: EdgeInsets.only(
                        left: ScreenUtil().setWidth(15),
                        right: ScreenUtil().setWidth(15)),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        // SizedBox(
                        //   height: ScreenUtil().setHeight(size * 0.1),
                        // ),
                        AutoSizeText(
                          S.of(context).no_spaces_left,
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(15),
                            fontWeight: FontWeight.bold,
                            color: Colors.white,
                            height: 0.7,
                          ),
                          maxLines: 1,
                          textAlign: TextAlign.center,
                          overflow: TextOverflow.ellipsis,
                        ),
                        isgem
                            ? Container(
                                
                                width: double.infinity,
                                child: Wrap(
                                  children: [
                                    GridView.builder(
                                        itemCount: gemList!.length,
                                        shrinkWrap: true,
                                        padding: EdgeInsets.only(top: 0),
                                        gridDelegate:
                                            SliverGridDelegateWithFixedCrossAxisCount(
                                          crossAxisCount: gemList!.length,
                                          //  mainAxisSpacing: ScreenUtil().setHeight(10), //每行间距
                                          // crossAxisSpacing: ScreenUtil().setWidth(10), //交叉轴每行间距
                                        ),
                                        itemBuilder: (context, index) {
                                          return Container(
                                              height:
                                                  ScreenUtil().setHeight(23),
                                              child: Row(
                                                //GridView.builder居中
                                                mainAxisAlignment:
                                                    MainAxisAlignment.center,
                                                children: [
                                                  Image.asset(
                                                      gemList![index].img,
                                                      width: ScreenUtil()
                                                          .setWidth(23),
                                                      height: ScreenUtil()
                                                          .setWidth(23),
                                                      fit: BoxFit.cover),
                                                  SizedBox(
                                                    width: 2,
                                                  ),
                                                  AutoSizeText(
                                                    '${gemList![index].gemCount}',
                                                    style: TextStyle(
                                                      fontWeight:
                                                          FontWeight.bold,
                                                      fontSize: ScreenUtil()
                                                          .setSp(13),
                                                      color: Color(0xffffffff),
                                                      height: 0.7,
                                                    ),
                                                    maxLines: 1,
                                                    textAlign: TextAlign.center,
                                                    overflow:
                                                        TextOverflow.ellipsis,
                                                  ),
                                                ],
                                              ));
                                        })
                                  ],
                                ),
                              )
                            : Column(
                                children: [
                                  SizedBox(
                                    height:
                                        ScreenUtil().setHeight(hight * 0.06),
                                  ),
                                  AutoSizeText(
                                    S.of(context).fraction_('$currentScore'),
                                    style: TextStyle(
                                      fontSize: ScreenUtil().setSp(20),
                                      fontWeight: FontWeight.bold,
                                      color: Color(0XffFEAE00),
                                      height: 0.7,
                                    ),
                                    maxLines: 1,
                                    textAlign: TextAlign.center,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ],
                              ),
                        SizedBox(
                          height: hight * 0.03,
                        ),
                      ],
                    )),
                //
              ),
                 SizedBox(
                        height: hight * 0.02,
                      ),
             getBottom(context,size,hight),

            ])));
  }

  Widget getBottom(BuildContext context,double width,double hight) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        GestureDetector(
          onTap: () {
            Navigator.pop(context);
            Navigator.pop(context);
          },
          child: Container(
              height: hight*0.05,
                   width: CommUtils.getISWidth(width, hight)?hight*0.15:width*0.17 ,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/new_game.png'),
                  fit: BoxFit.fill,
                ),
              ),
              alignment: Alignment.center,
              child: Padding(
                  padding:
                      EdgeInsets.only(top: 5, bottom: 7, right: 5, left: 5),
                  child: CommUtils.getTextGame(
                    context,
                    15,
                    S.of(context).no_,
                    Color(0xffe7f9c6),
                    Color(0Xff5afddd),
                  ))),
        ),
        GestureDetector(
          onTap: () {
            if (callback != null) {
              Navigator.pop(context);
              callback!(1);
            }
          },
          child: Container(
               height: hight*0.05,
               width: CommUtils.getISWidth(width, hight)?hight*0.3:width*0.35,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/images/new_game.png'),
                  fit: BoxFit.fill,
                ),
              ),
              alignment: Alignment.center,
              child: Padding(
                  padding:
                      EdgeInsets.only(top: 5, bottom: 7, right: 5, left: 5),
                  child: CommUtils.getTextGame(
                    context,
                    15,
                    S.of(context).retry_,
                    Color(0xffe7f9c6),
                    Color(0Xff5afddd),
                  ))),
        ),
      ],
    );
  }
}
